package com.ljx.offer;

import org.junit.Test;

/**
 * @author 李捷禧
 * Date: 2023/4/1
 * ClassName: C_26
 */

public class C_26 {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if(A == null || B == null) return false;
            if(A.val == B.val && (dfs(A.left,B.left) && dfs(A.right,B.right))) return true;
        return isSubStructure(A.left,B) || isSubStructure(A.right,B);
    }

    boolean dfs(TreeNode rootA, TreeNode rootB){
        if (rootB == null) return true;
        if(rootA == null) return false;
        if (rootA.val == rootB.val) {
            return dfs(rootA.left, rootB.left) && dfs(rootA.right , rootB.right);
        }else {
            return false;
        }
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
}
